
#include <thread>
#include <atomic>
#include <cstdio>
#include "param.h"
#include "macUtil.h"
#include "log/Logging.h"
#include "qlibc/FileUtils.h"
#include "common/httpUtil.h"
#include "accessPublishUtil.h"
#include "serviceRequestHandler.h"
#include "siteService/nlohmann/json.hpp"
#include "siteService/service_site_manager.h"

using namespace std;
using namespace httplib;
using namespace servicesite;
using namespace std::placeholders;
using json = nlohmann::json;

int main(int argc, char* argv[]) {
    LOG_RED << "-----------------------------------------";
    LOG_RED << "-----------------------------------------";
    LOG_RED << "---------------CONFIG_SITE START----------";
    LOG_RED << "-----------------------------------------";
    LOG_RED << "-----------------------------------------";

    
    if(argc = 1){
        LOG_RED << "Usage Error.....";
        LOG_PURPLE << "Try again with the format: [config <DirPath>]";
        return 0;
    }


    httplib::ThreadPool threadPool_(10);

    //设置配置文件加载路径
    configParamUtil* configPathPtr = configParamUtil::getInstance();
    configPathPtr->setConfigPath(string(argv[1]));

    ServiceSiteManager* serviceSiteManager = ServiceSiteManager::getInstance();
    serviceSiteManager->setServerPort(ConfigSitePort);
    serviceSiteManager->setSiteIdSummary(CONFIG_SITE_ID, CONFIG_SITE_ID_NAME);
    

    /**
     * 如果是安卓版本，首先获取面板配置文件，如果uid和mac为空，则获取uid和mac，并回写回去
     * 适用于电视、目前先删除此操作
    */
    #ifdef ANDROID_VERSION_DELETE
        qlibc::QData panelInfoData = configParamUtil::getInstance()->getPanelInfo();
        if(panelInfoData.getString("device_id").empty() || panelInfoData.getString("device_mac").empty()){
            Util util;
            Option option;
            util.get_uid(option);
            panelInfoData.setString("device_id", option.uid);
            panelInfoData.setString("device_mac", option.mac);
            panelInfoData.setString("paneltype", "androidTv");
            configParamUtil::getInstance()->setPanelInfo(panelInfoData);
        }
    #endif


// 提供的服务接口
    //配置面板配置信息
    serviceSiteManager->registerServiceRequestHandler(SETPANELINFO_REQUEST_SERVICE_ID, setPanelInfo_service_request_handler);

    //获取面板配置信息
    serviceSiteManager->registerServiceRequestHandler(GETPANELINFO_REQUEST_SERVICE_ID, getPanelInfo_service_request_handler);

    //注册虹美面板到云端 
    serviceSiteManager->registerServiceRequestHandler(HONGMEI_PANEL_REGISTER_REQUEST_SERVICE_ID, hongmeiPanelRegister_service_request_handler);


// 发布的消息
    serviceSiteManager->registerMessageId(PANELINFO_MODIFIED_MESSAGE_ID);     

// 注册消息处理函数
    serviceSiteManager->registerMessageHandler(Hongmei_GatewayInfo_MessageID, "hongmei_dev", hongmeiGatewayInfo_messageHandle); //网关属性消息
    

    // 站点监听线程启动
    threadPool_.enqueue([&](){
        while(true){
            //自启动方式
        //    int code = serviceSiteManager->start();
            //注册启动方式
            int code = serviceSiteManager->startByRegister();
            if(code != 0){
                LOG_RED << "===>configSite startByRegister error, code = " << code;
                LOG_RED << "===>configSite startByRegister in 3 seconds....";
                std::this_thread::sleep_for(std::chrono::seconds(3));
            }else{
                LOG_RED << "===>configSite startByRegister successfully.....";
                break;
            }
        }
    });


    //订阅虹美站点消息
    threadPool_.enqueue([&](){
        while(true){
            std::vector<string> messageIdList;
            messageIdList.push_back(Hongmei_GatewayInfo_MessageID);
            messageIdList.push_back(Hongmei_Unbind_MessageID);
            messageIdList.push_back(Hongmei_Destroy_Network_MessageID);
            //获取雷达站点ip
            int code = serviceSiteManager->subscribeMessage("127.0.0.1", 9021, "hongmei_dev", messageIdList);
            if (code == ServiceSiteManager::RET_CODE_OK) {
                LOG_PURPLE << "subscribeMessage hongmei_dev message ok....";
                return;
            }
            std::this_thread::sleep_for(std::chrono::seconds(10));
            LOG_RED << "subscribeMessage hongmei_dev Message failed....., start to subscribe in 10 seconds";
        }
    });

    while(true){
        std::this_thread::sleep_for(std::chrono::seconds(60 *10));
    }

    return 0;
}
